import 'package:dio/dio.dart';
import 'package:mi_mang_flutter/network/extra_keys.dart';
import '../model/public/entity_factory.dart';
import '../model/user_info_model.dart';
import '../network/response_data.dart';
import '../network/client.dart';

/// 获取用户信息接口
class UserApi {
  static const String API_USER_CURRENT = "/api/user/current";
  static const String API_USER_REGIST = "/api/user/register";
  static const String API_USER_EDIT_PASSWORD = "/api/user/edit_password";
  static const String API_USER_CLEAR_ACCOUNT = "/api/user/clear_account";
  static const String API_USER_EMAIL_CHANGE_PASSWORD =
      "/api/user/email_change_password";
  static const String API_USER_UPDATE_INFO = "/api/user/update_info";

  /// 获取用户信息
  /// [loading] 表示是否显示加载动画
  /// (初次请求是需要显示加载动画,静默刷新用户数据时不需要加载动画)
  static Future<ResponseData<UserInfoModel>> getUserCurrent(
    bool loading,
  ) async {
    var response = await client.get(
      API_USER_CURRENT,
      options: Options(extra: {
        ExtraKeys.NEED_LOADING: loading,
      }),
    );
    return ResponseData<UserInfoModel>(
      code: response.code,
      msg: response.msg,
      data: EntityFactory.generateOBJ(response.data),
    );
  }

  /// 注册用户
  ///
  /// [username] 用户名
  ///
  /// [password] 密码
  static Future<ResponseData> userRegist(
    String username,
    String password,
  ) async {
    var response = await client.formPost(
      API_USER_REGIST,
      data: {
        "username": username,
        "password": password,
      },
      options: Options(extra: const {
        ExtraKeys.NEED_TOKEN: false,
      }),
    );
    return ResponseData(
      code: response.code,
      msg: response.msg,
      data: EntityFactory.generateOBJ(response.data),
    );
  }

  /// 获取用户信息
  /// [loading] 表示是否显示加载动画
  /// (初次请求是需要显示加载动画,静默刷新用户数据时不需要加载动画)
  static Future<ResponseData> clearAccount() async {
    return client.delete(API_USER_CLEAR_ACCOUNT);
  }

  /// 修改密码
  ///
  /// [password] 原密码
  ///
  /// [newPassword] 新密码
  static Future<ResponseData> editPassword(
    String password,
    String newPassword,
  ) async {
    var response = await client.post(
      API_USER_EDIT_PASSWORD,
      data: {
        "password": password,
        "newPassword": newPassword,
      },
    );
    return ResponseData(
      code: response.code,
      msg: response.msg,
      data: EntityFactory.generateOBJ(response.data),
    );
  }

  /// 忘记密码(根据邮箱修改密码)
  ///
  /// [mobile] 手机号
  ///
  /// [code] 验证码
  ///
  /// [password] 新密码
  static Future<ResponseData> emailChangePassword(
    String mobile,
    String code,
    String password,
  ) async {
    var response = await client.formPost(
      API_USER_EMAIL_CHANGE_PASSWORD,
      data: {
        "email": mobile,
        "code": code,
        "password": password,
      },
      options: Options(extra: const {
        ExtraKeys.NEED_TOKEN: false,
      }),
    );
    return ResponseData(
      code: response.code,
      msg: response.msg,
      data: EntityFactory.generateOBJ(response.data),
    );
  }

  /// 忘记密码
  ///
  /// [avatar] 头像(上传后的图片id)
  ///
  /// [birthday] 出生日期
  ///
  /// [email] 邮箱
  ///
  /// [mobile] 手机号
  ///
  /// [nickName] 昵称
  ///
  /// [sex] 性别 0未知1男 2女
  ///
  /// [signature] 个性签名
  ///
  /// [theme] 主题
  static Future<ResponseData> updateUserInfo(
    String? avatar,
    DateTime? birthday,
    String? email,
    String? mobile,
    String? nickName,
    int? sex,
    String? signature,
    String? theme,
  ) async {
    var response = await client.post(
      API_USER_UPDATE_INFO,
      data: {
        "avatar": avatar,
        "birthday": birthday?.toIso8601String(),
        "email": email,
        "mobile": mobile,
        "nickName": nickName,
        "sex": sex,
        "signature": signature,
        "theme": theme,
      },
    );
    return ResponseData(
      code: response.code,
      msg: response.msg,
      data: EntityFactory.generateOBJ(response.data),
    );
  }
}
